package 代码随想2._3哈希表;

/**
 * @author XXX
 * @date 2024-01-14 21:30
 */

import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode.cn/problems/happy-number/
 */
public class _3快乐数 {
    public boolean isHappy(int n) {
        Set<Integer> record = new HashSet<>();
        while (n != 1 && !record.contains(n)){
            record.add(n);
            n = getNextNum(n);
        }

        return n==1;
    }

    int getNextNum(int n){
        int res = 0;
        while (n > 0){
            int tmp = n % 10;
            res += tmp * tmp;
            n /= 10;
        }
        return res;
    }
}
